<?php
// 本类由系统自动生成，仅供测试用途
class UserAction extends Action {


    public function index(){
	   $goods=M('goods');
	   import('ORG.Util.Page');
	   if(isset($_POST['chaname'])){$condition['name']=$_POST['chaname'];}//获取post提交过来的条件值
       $count=$goods->where($condition)->count();
	   $Page       = new Page($count,8);
	   $nowPage = isset($_GET['p'])?$_GET['p']:1;
	   $list=$goods->order('id desc')->where($condition)->limit($Page->firstRow.','.$Page->listRows)->select();
	   $Page->setConfig('theme', '%upPage%  %linkPage%  %downPage%');
	   $show       = $Page->show();
	   $config=array(
	     'url'=>'http://localhost/shop/home/Tpl',
	   );
	   $this->assign('config',$config);
	   $this->assign('page',$show);
	   $this->assign('list',$list);
       $this->display();
     
    }
	
	public function addshop(){
	  $id=$_GET['id'];
	  if(isset($_GET['id'])&&!empty($_GET['id'])){
	      $id=$_GET['id'];
          $car=cookie('cartshop');
		  $car[$id]=json_encode(array('id'=>$id,'num'=>1));
		  cookie('cartshop',$car);
		 }	   
	   $pageconfig=array(
	      'tittle'=>'商品加入购物车',
	   );
	   $this->assign('pageconfig',$pageconfig);
	   $this->display();
	}
	public function getshop(){  //获取购物车
	
	      $db=M('goods');
          $car=cookie('cartshop');
		  $total=0;
          foreach($car as $index=>$value){
		     $shop[$index]= json_decode($car[$index],true);
		     $condition['id']=$index;
			 $cartshop[$index]=$db->where($condition)->select();
			 $total=$total+$cartshop[$index][0]['value'];
		  }
	   C("total",$total);
	   $pageconfig=array(                     //参数
	      "tittle"=>"购物车",
		  "total_value"=>$total,
		  "loginstate"=>$_SESSION['loginstate'],
		  "cartshopnum"=>count($car),     //购物车里商品数量
		  "g_infor"=>$cartshop,
	   );

	   $this->assign("cartshop",$shop);
	   $this->assign("pageconfig",$pageconfig);	
	}
	public function cartshop(){  //展示购物车
       R('User/getshop');
	   $this->display();
	}
    public function changeshop(){
	   $action=$_POST['action'];
	   $id=$_POST['id'];
	   $sb=cookie('cartshop');
	   if($action=="del"){  //删除对应项的商品
	     unset($sb[$id]);
		 cookie('cartshop',$sb);
	   }else if($action=="changenum"){
	      echo $b[$id];
	   }
	} 
	
	
	public function order(){   //订单页面
     if(!empty($_SESSION['loginname'])&&isset($_SESSION['loginname'])&&$_SESSION['loginstate']==1){//判断是否已经登录
	   R('User/getshop');
	   $pageconfig['tittle']="订单填写";
	   //$this->assign("pageconfig",$pageconfig);
	   $this->display();
	   if(isset($_POST)&&!empty($_POST)){
	     date_default_timezone_set("Etc/Gmt-8");
	     $order=M('order');
	     $data['od_no']="xk".date("YmdsHis").rand(0,100).rand(0,100);//订单号
		 $data['od_date']=date("Y-m-d H:i:s");
		 $data['od_content']=$_POST['goodsmain'];//订单内容
		 $data['od_user']=$_SESSION['loginname'];
		 $data['od_value']=C('total');
		 $data['od_adress']=$_POST['address'];
		 $data['od_name']=$_POST['rename'];
		 $data['od_tel']=$_POST['retel'];
		 $data['od_email']=$_POST['remail'];
		 $data['od_payway']=$_POST['payselect'];
		 $data['od_sendway']=$_POST['sendselect'];
		 $data['od_status']=0;
		 //$order->add();
		 $order->add($data);
	   }
	  }else{
	    $headerurl=__APP__."/Index/login";
	    header('location:'.$headerurl);
	  }  
	}
	
	public function alipay(){
      header("Content-type:text/html;charset=utf-8");
      if(!empty($_SESSION['loginname'])&&isset($_SESSION['loginname'])&&$_SESSION['loginstate']==1){//判断是否已经登录
	   R('User/getshop');
	   if(isset($_POST)&&!empty($_POST)){  //如果post不为空
	     date_default_timezone_set("Etc/Gmt-8");
	     $order=M('order');
	     $data['od_no']="xk".date("YmdsHis").rand(0,100).rand(0,100);//订单号
		 $data['od_date']=date("Y-m-d H:i:s");
		 $data['od_content']=$_POST['goodsmain'];//订单内容
		 $data['od_user']=$_SESSION['loginname'];
		 $data['od_value']=C('total');
		 $data['od_adress']=$_POST['address'];
		 $data['od_name']=$_POST['rename'];
		 $data['od_tel']=$_POST['retel'];
		 $data['od_email']=$_POST['remail'];
		 $data['od_payway']=$_POST['payselect'];
		 $data['od_sendway']=$_POST['sendselect'];
		 if($data['od_payway']!=="货到付款"){
		    $data['od_status']=0;
		 }else{
		    if($data['od_sendway']=="上门自取"){
			  $data['od_status']=2;
			}else{
			  $data['od_status']=1;
			}
		 }
		 
		 $order->add($data);
		 cookie('cartshop',NULL);//收录订单后清空购物车
		 if($_POST['payselect']=="在线支付"){
		 
/*===========================支付宝页面====================================================*/	
	

/* *
 * 功能：纯担保交易接口接入页
 * 版本：3.3
 * 修改日期：2012-07-23
 * 说明：
 * 以下代码只是为了方便商户测试而提供的样例代码，商户可以根据自己网站的需要，按照技术文档编写,并非一定要使用该代码。
 * 该代码仅供学习和研究支付宝接口使用，只是提供一个参考。

 *************************注意*************************
 * 如果您在接口集成过程中遇到问题，可以按照下面的途径来解决
 * 1、商户服务中心（https://b.alipay.com/support/helperApply.htm?action=consultationApply），提交申请集成协助，我们会有专业的技术工程师主动联系您协助解决
 * 2、商户帮助中心（http://help.alipay.com/support/232511-16307/0-16307.htm?sh=Y&info_type=9）
 * 3、支付宝论坛（http://club.alipay.com/read-htm-tid-8681712.html）
 * 如果不想使用扩展功能请把扩展功能参数赋空值。
 */
require_once("alipay/alipay.config.php");
require_once("alipay/lib/alipay_submit.class.php");

/**************************请求参数**************************/

        //支付类型
        $payment_type = "1";
        //必填，不能修改
        //服务器异步通知页面路径
        $notify_url = "http://www.xiakenet.com/create_partner_trade_by_buyer-PHP-UTF-8/notify_url.php";
        //需http://格式的完整路径，不能加?id=123这类自定义参数

        //页面跳转同步通知页面路径
        $return_url = "http://www.xiakenet.com/create_partner_trade_by_buyer-PHP-UTF-8/return_url.php";
        //需http://格式的完整路径，不能加?id=123这类自定义参数，不能写成http://localhost/

        //商户订单号
        //$out_trade_no = $_POST['WIDout_trade_no'];
        $out_trade_no = $data['od_no'];
        //商户网站订单系统中唯一订单号，必填

        //订单名称
        //$subject = $_POST['WIDsubject'];
        $subject = '正在调试支付宝接口';
        //必填

        //付款金额
        //$price = $_POST['WIDprice'];
        $price = '0.10';
        //必填

        //商品数量
        $quantity = "1";
        //必填，建议默认为1，不改变值，把一次交易看成是一次下订单而非购买一件商品
        //物流费用
        $logistics_fee = "0.00";
        //必填，即运费
        //物流类型
        $logistics_type = "EXPRESS";
        //必填，三个值可选：EXPRESS（快递）、POST（平邮）、EMS（EMS）
        //物流支付方式
        $logistics_payment = "SELLER_PAY";
        //必填，两个值可选：SELLER_PAY（卖家承担运费）、BUYER_PAY（买家承担运费）
        //订单描述

        $body = @$_POST['WIDbody'];
        //商品展示地址
        $show_url = @$_POST['WIDshow_url'];
        //需以http://开头的完整路径，如：http://www.商户网站.com/myorder.html

        //收货人姓名
        //$receive_name = $_POST['WIDreceive_name'];
        $receive_name = '战三';
        //如：张三

        //收货人地址
        //$receive_address = $_POST['WIDreceive_address'];
        $receive_address = '福建省福州市台江区排位路256号';

        //如：XX省XXX市XXX区XXX路XXX小区XXX栋XXX单元XXX号

        //收货人邮编
        //$receive_zip = $_POST['WIDreceive_zip'];
        $receive_zip = '301500';


        //如：123456

        //收货人电话号码
        //$receive_phone = $_POST['WIDreceive_phone'];
        $receive_phone = '15859026673';

        //如：0571-88158090

        //收货人手机号码
        //$receive_mobile = $_POST['WIDreceive_mobile'];
        $receive_mobile ='15859026673';
        //如：13312341234


/************************************************************/

//构造要请求的参数数组，无需改动
$parameter = array(
		"service" => "create_partner_trade_by_buyer",
		"partner" => trim($alipay_config['partner']),
		"seller_email" => trim($alipay_config['seller_email']),
		"payment_type"	=> $payment_type,
		"notify_url"	=> $notify_url,
		"return_url"	=> $return_url,
		"out_trade_no"	=> $out_trade_no,
		"subject"	=> $subject,
		"price"	=> $price,
		"quantity"	=> $quantity,
		"logistics_fee"	=> $logistics_fee,
		"logistics_type"	=> $logistics_type,
		"logistics_payment"	=> $logistics_payment,
		"body"	=> $body,
		"show_url"	=> $show_url,
		"receive_name"	=> $receive_name,
		"receive_address"	=> $receive_address,
		"receive_zip"	=> $receive_zip,
		"receive_phone"	=> $receive_phone,
		"receive_mobile"	=> $receive_mobile,
		"_input_charset"	=> trim(strtolower($alipay_config['input_charset']))
);

//建立请求
$alipaySubmit = new AlipaySubmit($alipay_config);
$html_text = $alipaySubmit->buildRequestForm($parameter,"get", "确认");
echo'<div style="height:0px; overflow:hidden;">'.$html_text.'</div>';
echo'正在跳转支付页面...';

	
/*===========================支付宝页面====================================================*/			 
		 
		 
		 }else{$this->success('订单提交成功',U('Uc/index'),10);}
		  
	   }
	  }else{  //如果没有登录进入登录页面
	    $headerurl=__APP__."/Index/login";
	    header('location:'.$headerurl);
	  }  
	}
	
	
	
	public function shiyan(){
	   
     echo 'sd';
	       
     $this->display();
	}
	
}
?>